package cn.kxm.service.impl;

import java.util.Date;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import cn.kxm.dao.OfferBillDetailMapper;
import cn.kxm.dao.OfferPriceBillMapper;
import cn.kxm.pojo.OfferBillDetail;
import cn.kxm.pojo.OfferPriceBill;
import cn.kxm.service.OfferPriceBillService;
@Service
public class OfferPriceBillServiceImpl implements OfferPriceBillService {
	@Autowired
	private OfferPriceBillMapper opbm;
	@Autowired
	private OfferBillDetailMapper obdm;

	@Override
	public List<OfferPriceBill> getAllByCondition(String code, Date startTime,
			Date endTime, String customName, int offset, int size) {
		return opbm.getAllByCondition(code, startTime, endTime, customName, offset, size);
	}

	@Override
	public int getCountByCondition(String code, Date startTime, Date endTime,
			String customName) {
		return opbm.getCountByCondition(code, startTime, endTime, customName);
	}

	@Override
	public OfferPriceBill getOfferPriceBill(String code) {
		return opbm.getOfferPriceBill(code);
	}

	@Override
	public int insertOfferPriceBill(OfferPriceBill offerPriceBill) {
		return opbm.insertOfferPriceBill(offerPriceBill);
	}

	@Override
	public boolean insertOfferPriceBill(OfferPriceBill offerPriceBill,
			List<OfferBillDetail> offerBillDetails) {
		int temp01=insertOfferPriceBill(offerPriceBill);
		int temp02=0;
		for (OfferBillDetail offerBillDetail : offerBillDetails) {
			obdm.insertOfferBillDetail(offerBillDetail);
			temp02++;
		}
		if(temp01==1&&temp02==offerBillDetails.size()){
			return true;
		}else{
			return false;
		}
	}

	@Override
	public int updateOfferPriceBill(OfferPriceBill offerPriceBill) {
		return opbm.updateOfferPriceBill(offerPriceBill);
	}

	@Override
	public boolean updateOfferPriceBill(OfferPriceBill offerPriceBill,
			List<OfferBillDetail> offerBillDetails) {
		int temp01=updateOfferPriceBill(offerPriceBill);
		int temp02=0;
		if(obdm.deleteByBillCode(offerPriceBill.getCode())>0){
			temp02++;
		}
		for (OfferBillDetail offerBillDetail : offerBillDetails) {
			obdm.insertOfferBillDetail(offerBillDetail);
			temp02++;
		}
		if(temp01==1&&temp02==offerBillDetails.size()+1){
			return true;
		}else{
			return false;
		}
	}

	@Override
	public int deleteOfferPriceBill(String code) {
		return opbm.deleteOfferPriceBill(code);
	}

	@Override
	public boolean deleteOfferPriceBillAndDetails(String code) {
		int temp02=obdm.deleteByBillCode(code);
		int temp01=deleteOfferPriceBill(code);
		return temp01==1&&temp02>0;
	}

	@Override
	public boolean audit(String code) {
		return opbm.audit(code)==1;
	}

	@Override
	public boolean revoke(String code) {
		return opbm.revoke(code)==1;
	}

}
